package com.lovepick;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKPoiInfo;
import com.lovepick.adapter.PoiInfoAdapter;
import com.lovepick.bean.Order;
import com.lovepick.map.MapHelper;

public class PoiDialogActivity extends Activity
{
	protected static final String TAG = "PoiDialog";
	protected static final String CITY = "北京";
	private EditText mEditText;
	private ListView mListView;
	private TextView mTextView;
	private ProgressBar mProgressBar;

	private MapHelper mMapHelper = null;
	private PoiHandler mHandler = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_poi);
		mEditText = (EditText) this.findViewById(R.id.dialog_poi_input);
		mEditText.addTextChangedListener(mTextWatcher);
		mListView = (ListView) this.findViewById(R.id.dialog_poi_result);
		mTextView = (TextView) this.findViewById(R.id.dialog_poi_none);
		mProgressBar= (ProgressBar) this.findViewById(R.id.dialog_poi_progress);
		mTextView.setVisibility(View.GONE);
		mListView.setVisibility(View.GONE);
		mProgressBar.setVisibility(View.GONE);
		if (getIntent() != null)
		{
			String pointType = getIntent().getStringExtra(Order.POINT_TYPE);
			if (pointType.equals(Order.SRC))
			{
				setTitle(R.string.label_poidialog_src);
				mEditText.setHint(R.string.src);
			}
			else
			{
				setTitle(R.string.label_poidialog_dest);
				mEditText.setHint(R.string.dest);
			}
		}
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

		mHandler = new PoiHandler(this);
		GlobalApp.getInstance().getMapManager().start();
		mMapHelper = new MapHelper(GlobalApp.getInstance().getMapManager(), mHandler);
	}

	private TextWatcher mTextWatcher = new TextWatcher()
	{

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count)
		{
			// TODO Auto-generated method stub
			if (s.length() >= 2)
			{
				mProgressBar.setVisibility(View.VISIBLE);
				mMapHelper.searchPoi(CITY, s.toString());
			}
			else
			{
				mListView.setVisibility(View.GONE);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after)
		{
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s)
		{
			// TODO Auto-generated method stub

		}
	};

	private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener()
	{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			// TODO Auto-generated method stub
			Log.i(TAG, "poi item click");
			MKPoiInfo info = (MKPoiInfo) parent.getItemAtPosition(position);
			String poiName = info.name;
			GeoPoint point = info.pt;
			mEditText.setText(poiName);
			Intent result = new Intent();
			result.putExtra(Order.ADDR_NAME, poiName);
			result.putExtra(Order.LAT, point.getLatitudeE6()/1E6);
			result.putExtra(Order.LNG, point.getLongitudeE6()/1E6);
			setResult(RESULT_OK, result);
			finish();
		}
	};

	private static class PoiHandler extends Handler
	{
		private WeakReference<PoiDialogActivity> mReference;
		private PoiDialogActivity mDialog;

		public PoiHandler(PoiDialogActivity dialog)
		{
			mReference = new WeakReference<PoiDialogActivity>(dialog);
		}

		@Override
		public void handleMessage(Message msg)
		{
			mDialog = mReference.get();

			switch (msg.what)
			{
				case MapHelper.POI_COMPLETE:
					mDialog.mProgressBar.setVisibility(View.GONE);
					ArrayList<MKPoiInfo> infos = mDialog.mMapHelper.getPoiResult().getAllPoi();
					if (infos != null)
					{
						PoiInfoAdapter adapter = new PoiInfoAdapter(mDialog, infos);
						mDialog.mListView.setAdapter(adapter);
						mDialog.mListView.setOnItemClickListener(mDialog.mItemClickListener);
						mDialog.mListView.setVisibility(View.VISIBLE);
					}
					else
					{
						mDialog.mTextView.setVisibility(View.VISIBLE);
						mDialog.mTextView.setText(R.string.poi_res_fail);
					}
					break;

				default:
					break;
			}
		}
	}

}
